
/**
 * 行为转发类
 * @constructor
 */
function RoomList(){

    /**
     * 动作转发
     * @param action
     * @param msg
     */
    this.getAction=function (msg) {

        message= JSON.parse(msg.data);

        var state=this.checkStatus(message.head);
        if(state==false){
            layer.msg(message.msg);
            return;
        }

        switch (message.head){

            /**
             * 获取列表
             */
            case ProtocolApi.Protocol['DDZ_GET_ROOM_LIST']:
                this.GetRoomList(message.msg);
                break;

            /**
             * 加入
             */
            case ProtocolApi.Protocol['DDZ_JION_ROOM']:
                this.PlayerJionRoom(message.msg);
                break;
        }
    };

    /**
     * 状态码检测
     * @param status
     * @returns {boolean}
     */
    this.checkStatus=function (status){
        if(status==ProtocolApi.Protocol['TOKEN_ERROR']){

            localStorage.setItem('PhpGameSocketToken','');
            localStorage.setItem('PhpGameSocketTokenTime','');
            return false;
        }

        if(status===ProtocolApi.Protocol['GAME_ERROR']){
            return false;
        }
        return true;
    };

    /**
     * 获取列表
     * @constructor
     */
    this.GetRoomList=function (msg){
       var RoomList=msg.data.RoomList;
       console.log(RoomList);
       var htmlContent='';
       for(var i=0;i<RoomList.length;i++){
           htmlContent=htmlContent+'<li class="RoomID" id="Room'+RoomList[i]['roomID']+'" Roomid="'+RoomList[i]['roomID']+'">';
           htmlContent=htmlContent+'<div class="roomHead">ID：'+RoomList[i]['roomID']+'</div>';
           htmlContent=htmlContent+'<div class="roomBody">';
           htmlContent=htmlContent+'<p>状态：<span id="RoomStatus'+RoomList[i]['roomID']+'">'+(RoomList[i]['roomStatus']==1?'游戏中':'等待')+'</span></p>';
           htmlContent=htmlContent+'<p>人数：<span id="RoomNumber'+RoomList[i]['roomID']+'">'+RoomList[i]['playerNumber']+'</span></p>';
           htmlContent=htmlContent+'</div>';
           htmlContent=htmlContent+'</li>';
       }
       $('#bodyul').html(htmlContent);

       //事件绑定
        $('.RoomID').click(function (){
            roomID=$(this).attr('Roomid');
            var message={roomID:roomID};
            ws.send(ProtocolApi.ProtocolMsg(ProtocolApi.Protocol['DDZ_JION_ROOM'],message));
        });

    };

    /**
     * 加入
     * @constructor
     */
    this.PlayerJionRoom=function (msg){
        localStorage.setItem('PhpGameSocketRoomid', msg.RoomID);
        $('#RoomNumber'+msg.RoomID).html((parseInt($('#RoomNumber'+msg.RoomID).html())+1));
        layer.msg('加入房间成功');
    }



}